package com.tao.ai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.tao.ai.entity.Coursestudent;
import com.tao.ai.mapper.CourseMapper;
import com.tao.ai.mapper.CourseStudentMapper;
import com.tao.ai.mapper.UserMapper;
import com.tao.ai.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private CourseStudentMapper courseStudentMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public int signStudent(String courseName, Integer studentId) {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("course_name",courseName);

        int courseId=courseMapper.selectOne(wrapper).getCourseId();
        QueryWrapper wrapper1=new QueryWrapper();
        wrapper1.eq("course_id",courseId);
        wrapper1.eq("student_id",studentId);

        Coursestudent courseStudent=courseStudentMapper.selectOne(wrapper1);
        if(courseStudent.getSign()==1){
            return 0;
        }
        else{
            courseStudent.setSign(1);
            UpdateWrapper<Coursestudent> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("course_id",courseId).eq("student_id",studentId).set("sign", 1);
            courseStudentMapper.update(null, updateWrapper);
            return 1;
        }

    }

    @Override
    public int signStudent2(String courseName, String studentName) {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("course_name",courseName);

        QueryWrapper wrapper3=new QueryWrapper();
        wrapper.eq("username",studentName);

        int courseId=courseMapper.selectOne(wrapper).getCourseId();
        int studentId=userMapper.selectOne(wrapper3).getUserId();

        QueryWrapper wrapper1=new QueryWrapper();
        wrapper1.eq("course_id",courseId);
        wrapper1.eq("student_id",studentId);

        Coursestudent courseStudent=courseStudentMapper.selectOne(wrapper1);
        if(courseStudent.getSign()==1){
            return 0;
        }
        else{
            courseStudent.setSign(1);
            UpdateWrapper<Coursestudent> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("course_id",courseId).eq("student_id",studentId).set("sign", 1);
            courseStudentMapper.update(null, updateWrapper);
            return 1;
        }
    }
}
